package com.googlecode.stockhamster.rumor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class Rumor {
	private String effectCategory;
	private String effectCompany;
	private double effectRatio;
	private double ratio1;
	private double ratio2;
	private double ratio3;
	private int ID;

	private static PreparedStatement state = null;
	private static ResultSet rs = null;

	public Rumor(String category, String company, double effectRadio,
			double ratio1, double ratio2, double ratio3) {
		this.effectCategory = category;
		this.effectCompany = company;
		this.effectRatio = effectRadio;
		this.ratio1 = ratio1;
		this.ratio2 = ratio2;
		this.ratio3 = ratio3;
	}

	public boolean isEffectCategory() {
		return this.effectCategory != null;
	}

	public String getEffectCategory() {
		return this.effectCategory;
	}

	public boolean isEffectCompany() {
		return this.effectCompany != null;
	}

	public String getEffectCompany() {
		return this.effectCompany;
	}

	public double getEffectRatio() {
		return effectRatio;
	}

	public int getID() {
		return ID;
	}

	public void setID(int ID) {
		this.ID = ID;
	}

	public double getRatio1() {
		return this.ratio1;
	}

	public double getRatio2() {
		return this.ratio2;
	}

	public double getRatio3() {
		return this.ratio3;
	}

	// for database
	// main keyword : id
	public boolean insert() {
		// insert this
		boolean flag = false;
		try {
			String sqlAddAmount = "insert into Rumor(effectCategory, effectCompany, effectRatio, ratio1, ratio2, ratio3) values(?,?,?,?,?,?)";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlAddAmount);
			state.setString(1, this.effectCategory);
			state.setString(2, this.effectCompany);
			state.setDouble(3, this.effectRatio);
			state.setDouble(4, this.ratio1);
			state.setDouble(5, this.ratio2);
			state.setDouble(6, this.ratio3);
			state.executeUpdate();
			flag = true;
			state.close();
		} catch (Exception ex) {
			ex.printStackTrace();
			flag = false;
		} finally {
			if (state != null) {
				if (state != null) {
					try {
						state.close();
					} catch (SQLException e) {
					}
				}
			}
		}
		return flag;
	}

	public static Rumor get(int id) {
		// return rumor whose id = id
		Rumor tempRumor = new Rumor(null, null, 0, 0, 0, 0);
		try {
			String sqlGetAccount = "select * from Rumor where ID=?";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccount);
			state.setInt(1, id);
			rs = state.executeQuery();
			if (!rs.next())
				return null;
			tempRumor = new Rumor(rs.getString(1), rs.getString(2), rs
					.getDouble(3), rs.getDouble(4), rs.getDouble(5), rs
					.getDouble(6));
			tempRumor.setID(rs.getInt(4));
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return tempRumor;
	}

	public static Integer[] getRumors(int startID) {
		// return all rumors with id >= startID
		Integer[] targetList = new Integer[0];
		try {
			ArrayList<Rumor> tempAccount = new ArrayList<Rumor>();
			String sqlGetAccountList = "select * from Rumor where id>? order by id";
			state = ConnectDatabase.getConnection().prepareStatement(
					sqlGetAccountList);
			state.setInt(1, startID);
			rs = state.executeQuery();
			while (rs.next()) {
				Rumor ac = new Rumor(null, null, 0, 0, 0, 0);
				ac = new Rumor(rs.getString(1), rs.getString(2), rs
						.getDouble(3), rs.getDouble(4), rs.getDouble(5), rs
						.getDouble(6));
				ac.setID(rs.getInt(4));
				tempAccount.add(ac);
			}
			targetList = new Integer[tempAccount.size()];
			for (int i = 0; i < tempAccount.size(); i++)
				targetList[i] = tempAccount.get(i).ID;
			state.close();
			rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (state != null) {
					state.close();
				}
				if (rs != null) {
					rs.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return targetList;
	}
}
